package com.example.administrator.pandanotes;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import org.litepal.crud.DataSupport;
/**
 * Created by 于博 on 2018-06-07.
 * 用户界面，可以显示用户信息，点击修改按钮可已进行用户信息修改
 */
public class UserActivity extends AppCompatActivity {
    //变量***********************************************************************
    User theUser = null;
    private TextView tVUserID = null;
    private EditText eTUserEditName = null;
    private EditText eTUserEditPwd = null;
    private EditText eTUserEditConPwd = null;
    private RadioButton rBMan = null;
    private RadioButton rBWoman = null;
    private EditText eTUserSign = null;
    private Button   btnUserEditCommit = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        //初始化及绑定***************************************************************
        tVUserID = (TextView) findViewById(R.id.tVUserEditID);
        eTUserEditName = (EditText) findViewById(R.id.eTUserEditName);
        eTUserEditPwd = (EditText) findViewById(R.id.eTUserEditPwd);
        eTUserEditConPwd = (EditText)findViewById(R.id.eTUserConPwd);
        eTUserSign = (EditText)findViewById(R.id.eTUserSign);
        rBMan = (RadioButton)findViewById(R.id.rBMan);
        rBWoman = (RadioButton)findViewById(R.id.rBWoman);
        btnUserEditCommit = (Button)findViewById(R.id.btnUserEditCommit);
        theUser = (User)getIntent().getSerializableExtra("theUser");
        Log.d("当前用户：",theUser.getUser_name());
        //设置值
        tVUserID.setText(theUser.getUser_id());
        eTUserEditName.setText(theUser.getUser_name());
        eTUserEditPwd.setText(theUser.getUser_pwd());
        eTUserEditConPwd.setText(theUser.getUser_pwd());
        if(theUser.getUser_sex().equals("男"))rBMan.setChecked(true);
        else rBWoman.setChecked(true);
        eTUserSign.setText(theUser.getUser_signature());
        //函数事件*******************************************************************
        rBMan.setOnClickListener(new View.OnClickListener() {//“男”单选按钮选中事件
            @Override
            public void onClick(View v) {
                rBWoman.setChecked(false);
            }
        });
        rBWoman.setOnClickListener(new View.OnClickListener() {//“女”单选按钮选中事件
            @Override
            public void onClick(View v) {
                rBMan.setChecked(false);
            }
        });
        //修改按钮点击事件，进行用户信息的修改，显示修改成功与否信息
        btnUserEditCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //数据审核及异常处理
                if(eTUserEditName.getText().toString().length()<1||eTUserEditName.getText().toString().length()>16)
                {
                    Toast.makeText(UserActivity.this, "昵称为1-16位字符或数字组成", Toast.LENGTH_SHORT).show();
                }
                else if(eTUserEditPwd.getText().toString().length()<8||eTUserEditPwd.getText().toString().length()>16)
                {
                    Toast.makeText(UserActivity.this, "密码为8-16位字符或数字组成", Toast.LENGTH_SHORT).show();
                }
                else if(eTUserSign.getText().toString().length()<0||eTUserSign.getText().toString().length()>20)
                {
                    Toast.makeText(UserActivity.this, "签名长度不符合要求", Toast.LENGTH_SHORT).show();
                }
                else if (eTUserEditPwd.getText().toString().equals(eTUserEditConPwd.getText().toString())) {
                    ContentValues values = new ContentValues();
                    values.put("user_name",eTUserEditName.getText().toString());
                    if (rBMan.isChecked())values.put("user_sex","男");
                    else values.put("user_sex","女");
                    values.put("user_pwd",eTUserEditPwd.getText().toString());
                    values.put("user_signature",eTUserSign.getText().toString());
                    DataSupport.updateAll(User.class, values, "user_id = ?",theUser.getUser_id());
                    Toast.makeText(UserActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(UserActivity.this,MainActivity.class);//跳转主页面
                    startActivity(intent);
                }
                else
                {
                    Toast.makeText(UserActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
